package cn.tju.leavingsystem.web;

import cn.tju.leavingsystem.entity.Staff;
import cn.tju.leavingsystem.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private StaffService staffService;

    @PostMapping("/")
    private Map<String, Object> login(@ModelAttribute Staff staff){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        String token = staff.getStaffId() + "." + staff.getPassword()
                + "." + staffService.getStaffById(staff.getStaffId()).getLevel();
        if(staffService.getStaffById(staff.getStaffId()).getPassword().equals(staff.getPassword())){
            modelMap.put("code", 0);
            modelMap.put("message", "登录成功");
            modelMap.put("token", token);
        }else{
            modelMap.put("code", -1);
            modelMap.put("message", "密码错误");
        }
        return modelMap;
    }
}
